package com.group.itms.routes.activities;

import java.sql.SQLException;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.group.itms.routes.DatabaseHelper;
import com.group.itms.routes.R;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.Topic;
import com.group.itms.routes.services.SyncManager;
import com.group.itms.routes.services.SyncService;
import com.group.itms.routes.services.SyncService.SyncListener;
import com.j256.ormlite.android.apptools.OpenHelperManager;

public abstract class BaseActivity extends SherlockFragmentActivity implements
		SyncListener {

	public static final String OBJECT_ID = "object_id";

	protected SyncManager mSyncManager;
	private DatabaseHelper mHelper;
	private boolean mIsHome;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mSyncManager = SyncManager.getInstance();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			if (!mIsHome) {
				goHome();
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void goHome() {
		Intent i = new Intent(this, MainActivity.class);
		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(i);
	}

	protected Topic getTopic(int id) {
		try {
			return getHelper().getTopicDao().queryForId(id);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	protected BObject getObject(int objId) {
		try {
			return getHelper().getObjectDao().queryForId(objId);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	protected void refreshObject(BObject object) {
		try {
			getHelper().getObjectDao().refresh(object);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	protected abstract String getTag();

	@Override
	public void onSyncDone(int statusCode) {
		String msg;
		switch (statusCode) {
		case SyncService.SYNC_OK:
			msg = "Synchronization completed succesfully";
			break;
		case SyncService.SYNC_IO_EXCEPTION:
			msg = "Synchronization completed with errors: network issue";
			break;
		case SyncService.SYNC_JSON_EXCEPTION:
			msg = "Synchronization completed with errors: unable to parse data";
			break;
		case SyncService.SYNC_SQLITE_ERROR:
			msg = "Synchronization completed with errors: unable to save data";
			break;
		default:
			msg = null;
		}
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	public void setAsHome() {
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		mIsHome = true;
	}

	public DatabaseHelper getHelper() {
		if (mHelper == null) {
			throw new RuntimeException(getClass()
					+ " onCreate has not called yet");
		}
		return mHelper;
	}

	public void startActivity(Class<?> cls) {
		startActivity(cls, true);
	}

	public void startActivity(Class<?> cls, boolean enableAnimations) {
		startActivity(new Intent(this, cls), enableAnimations);
	}

	public void startActivity(Intent intent, boolean enableAnimations) {
		startActivity(intent);
		if (enableAnimations) {
			overridePendingTransition(R.anim.enter_slide_left,
					R.anim.leave_slide_left);
		}
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.enter_slide_right,
				R.anim.leave_slide_right);
	}

	public boolean hasConnection() {
		    ConnectivityManager cm = (ConnectivityManager) getSystemService(
		        Context.CONNECTIVITY_SERVICE);
		    
		    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		    if (wifiNetwork != null && wifiNetwork.isConnected()) {
		    	return true;
		    }

		    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		    if (mobileNetwork != null && mobileNetwork.isConnected()) {
		    	return true;
		    }

		    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
		    if (activeNetwork != null && activeNetwork.isConnected()) {
		      return true;
		    }

		    return false;
		  }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		OpenHelperManager.releaseHelper();
	}

}
